/*eslint-disable*/
import axios from "axios";

// 基于arcgisserver的地图点选查询
export function serverIdentify(clickpoint, requestUrl, dataFun, polygonFun) {
    // 获取点击位置笛卡尔坐标
    var position = scene.pickPosition(clickpoint);
    if (!position) {
        position = Cesium.Cartesian3.fromDegrees(0, 0, 0);
    }
    // 从笛卡尔坐标获取经纬度
    var cartographic = Cesium.Cartographic.fromCartesian(position);
    var longitude = Cesium.Math.toDegrees(cartographic.longitude);
    var latitude = Cesium.Math.toDegrees(cartographic.latitude);
    // 查询arcgisserver
    axios({
        method: "get",
        url: requestUrl + "/MapServer/identify",
        params: {
            geometry: longitude + "," + latitude,
            geometryType: "esriGeometryPoint",
            sr: 4490,
            tolerance: 2,
            mapExtent: "-127.96,25.577,-65.074,51.2983251993735",
            imageDisplay: "60055096",
            returnGeometry: false,
            returnZ: false,
            returnM: false,
            returnUnformattedValues: false,
            returnFieldName: false,
            f: "json",
        },
    })
        .then((res) => {
            // 读取属性表
            let result = res.data.results[0].attributes
            // 属性信息处理
            dataFun(result)
            // 根据fid查询多边形轮廓
            return axios({
                method: "get",
                url: requestUrl + "/MapServer/0/query",
                params: {
                    where: 'FID="' + result.FID + '"',
                    geometryType: "esriGeometryEnvelope",
                    spatialRel: "esriSpatialRelIntersects",
                    returnGeometry: true,
                    returnTrueCurves: false,
                    returnIdsOnly: false,
                    returnCountOnly: false,
                    returnZ: false,
                    returnM: false,
                    returnDistinctValues: false,
                    returnExtentsOnly: false,
                    outFields: "*",
                    f: "json",
                },
            })
        })
        .then((res) => {
            // 整理坐标
            var points = new Array(0);
            res.data.features[0].geometry.rings[0].forEach((point) => {
                points.push(point[0]);
                points.push(point[1]);
            });
            // 多边形绘制
            polygonFun(points)
        })

}

// 删除某类poi标识
export function deleteFeature(keyText) {
    let entities = viewer.entities._entities._array;
    for (let i = 0; i < entities.length; i++) {
        if (
            entities[i]._name != undefined &&
            entities[i]._name.indexOf(keyText) != -1
        ) {
            viewer.entities.remove(entities[i]);
            i--;
        }
    }
}